import { Injectable } from '@nestjs/common';
import { User } from './user.interface';
import { UserEntity } from './user.entity';
import { Repository, createConnection } from 'typeorm';
import { InjectRepository } from '@nestjs/typeorm';

@Injectable()
export class UserService {
    constructor(
        @InjectRepository(UserEntity)
        private userRepository: Repository<UserEntity>
    ){}

    async create(user: User): Promise<void> {
        await this.userRepository.save(user);
    }

    async findUser(user): Promise<any> {
        let result = await this.userRepository.findOne(user);
        if(result) {
            return {
                code: '1',
                msg: 'success'
            }
        } else {
            return {
                code: '0',
                msg: 'fail'
            }
        }
    }

    findAll(): Promise<UserEntity[]> {
        return this.userRepository.find();
    }

    async remove(id: string): Promise<void> {
        await this.userRepository.delete(id);
    }
}
